const express = require('express')
const passport = require('passport')
const router = express.Router()
const Profile = require('../../models/Profile')

// test api
// router.get('/test', (req, res) => {
//   res.json({
//     message: 'profile page'
//   })
// })

/**
 * @router POST /api/profile/add
 * @description 创建信息接口
 * @access private
 */
router.post('/add', passport.authenticate('jwt', {
  session: false
}), (req, res) => {
  const profileFields = {}

  if (req.body.type) profileFields.type = req.body.type
  if (req.body.describe) profileFields.describe = req.body.describe
  if (req.body.income) profileFields.income = req.body.income
  if (req.body.expend) profileFields.expend = req.body.expend
  if (req.body.cash) profileFields.cash = req.body.cash
  if (req.body.remark) profileFields.remark = req.body.remark
  if (req.body.date) profileFields.date = req.body.date

  new Profile(profileFields).save()
    .then(profile => {
      res.json(profile)
    })
    .catch(err => {
      throw err
    })
})

/**
 * @router GET /api/profile/ /
 * @description 获取所有信息
 * @access private
 */
router.get('/', passport.authenticate('jwt', {
  session: false
}), (req, res) => {
  Profile.find()
    .then(profiles => {

      if (!profiles) {
        return res.status(404).json({
          message: '没有任何内容'
        })
      }

      res.json(profiles)
    })
    .catch(err => {
      return res.status(404).json(err)
    })
})

/**
 * @router GET /api/profile/:id
 * @description 获取单个信息
 * @access private
 */
router.get('/:id', passport.authenticate('jwt', {
  session: false
}), (req, res) => {
  Profile.findOne({
      _id: req.params.id
    })
    .then(profile => {

      if (!profile) {
        return res.status(404).json({
          message: '没有找到相应内容'
        })
      }

      return res.json(profile)
    })
    .catch(err => {
      return res.status(404).json(err)
    })
})

/**
 * @router POST /api/profile/edit
 * @description 创建信息接口
 * @access private
 */
router.post('/edit/:id', passport.authenticate('jwt', {
  session: false
}), (req, res) => {
  const profileFields = {}

  if (req.body.type) profileFields.type = req.body.type
  if (req.body.describe) profileFields.describe = req.body.describe
  if (req.body.income) profileFields.income = req.body.income
  if (req.body.expend) profileFields.expend = req.body.expend
  if (req.body.cash) profileFields.cash = req.body.cash
  if (req.body.remark) profileFields.remark = req.body.remark
  if (req.body.date) profileFields.date = req.body.date

  Profile.findOneAndUpdate({
      _id: req.params.id
    }, {
      $set: profileFields
    }, {
      new: true
    })
    .then(profile => {
      return res.json(profile)
    })
    .catch(err => {
      throw err
    })
})

/**
 * @router delete /api/profile/delete/:id
 * @description 删除接口信息
 * @access private
 */
router.delete('/delete/:id', passport.authenticate('jwt', {
  session: false
}), (req, res) => {
  Profile.findOneAndRemove({
      _id: req.params.id
    })
    .then(profile => {
      profile.save()
        .then(profile => {
          res.status(200).json(profile)
        })
        .catch(err => {
          res.status(400).json({
            message: '删除失败'
          })
        })
    })
    .catch(err => {
      throw err
    })
})




module.exports = router